package com.lanou3g.mynewstest.activity;


import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.os.Bundle;


import android.view.View;
import android.widget.ImageView;
import android.widget.TabHost;
import android.widget.TextView;

import com.lanou3g.mynewstest.R;
import com.lanou3g.mynewstest.base.MyBaseActivity;
import com.lanou3g.mynewstest.module.news.NewsFragment;
import com.lanou3g.mynewstest.module.topic.TopicFragment;
import com.lanou3g.mynewstest.module.person.PersonFragment;
import com.lanou3g.mynewstest.module.videos.VideoFragment;

import cn.jpush.android.api.JPushInterface;


public class MainActivityMy extends MyBaseActivity {
    private TabHost tab;
    private FragmentManager fm;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        int liners[]={R.id.news_liner,R.id.video_liner,R.id.topic_liner,R.id.person_liner};
        int selectors[]={R.drawable.news_tab,R.drawable.video_tab,R.drawable.topic_tab,R.drawable.person_tab};
        String text[]={"新闻","视听","话题","我"};
        tab = (TabHost) findViewById(android.R.id.tabhost);
        tab.setup();

        addTabs(liners,selectors,text);

        fm = getSupportFragmentManager();
        FragmentTransaction transaction = fm.beginTransaction();
        transaction.replace(R.id.news_liner, new NewsFragment());
        transaction.replace(R.id.video_liner, new VideoFragment());
        transaction.replace(R.id.topic_liner, new TopicFragment());
        transaction.replace(R.id.person_liner, new PersonFragment());

        transaction.commit();

    }
    public void addTabs(int []liners,int[]selectors,String[]text){
        for (int i = 0; i < liners.length; i++) {
            View view = getLayoutInflater().inflate(R.layout.item_main_tabs, null);
            ImageView imageView= (ImageView) view.findViewById(R.id.item_main_tab_iv);
            TextView textView= (TextView) view.findViewById(R.id.item_main_tab_tv);
            imageView.setImageResource(selectors[i]);
            textView.setText(text[i]);
            TabHost.TabSpec tabSpec = tab.newTabSpec("");
            tabSpec.setIndicator(view);
            tabSpec.setContent(liners[i]);
            tab.addTab(tabSpec);
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        JPushInterface.onResume(getBaseContext());
    }

    @Override
    protected void onPause() {
        super.onPause();
        JPushInterface.onPause(getBaseContext());
    }
}
